package view.manager;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import base.NumberGeneration;
import dao.DepartmentDao;
import model.Department;
import util.StringUtil;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.Rectangle;

public class DepartmentAddInterfrm extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JTextField textField;
	private JTextField textField_1;
	private JTextArea textArea;
	private JButton btnNewButton;
	private JButton btnNewButton_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DepartmentAddInterfrm frame = new DepartmentAddInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DepartmentAddInterfrm() {
		getContentPane().setEnabled(false);
		setClosable(true);
		setTitle("学院添加");
		setBounds(100, 100, 450, 300);
		lblNewLabel = new JLabel("学院编号：");
		lblNewLabel.setIcon(new ImageIcon(DepartmentAddInterfrm.class.getResource("/images/num.png")));
		
		lblNewLabel_1 = new JLabel("学院名称：");
		lblNewLabel_1.setIcon(new ImageIcon(DepartmentAddInterfrm.class.getResource("/images/name.png")));
		
		lblNewLabel_2 = new JLabel("学院描述：");
		lblNewLabel_2.setIcon(new ImageIcon(DepartmentAddInterfrm.class.getResource("/images/describe.png")));
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		textArea = new JTextArea();
		textArea.setBorder(new LineBorder(new Color(127,157,185), 1, false));
		
		btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				if (StringUtil.isEmpty(textField.getText())) {
//					JOptionPane.showMessageDialog(null, "学院编号不能为空", "提示", JOptionPane.ERROR_MESSAGE);
//					textField.requestFocus();
//					return;
//				}
//				if (!StringUtil.isNum(textField.getText())) {
//					JOptionPane.showMessageDialog(null, "学院编号需为数字", "提示", JOptionPane.ERROR_MESSAGE);
//					textField.requestFocus();
//					return;
//				}
//				if (textField.getText().length() >= 9) {
//					JOptionPane.showMessageDialog(null, "学院编号太长", "提示", JOptionPane.ERROR_MESSAGE);
//					textField.requestFocus();
//					textField.selectAll();
//					return;
//				}
				
				
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "学院名称不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					return;
				}
				if (textField_1.getText().length() >= 30) {
					JOptionPane.showMessageDialog(null, "学院名称太长", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					textField_1.selectAll();
					return;
				}
				if (textArea.getText().length() >= 50) {
					JOptionPane.showMessageDialog(null, "学院描述太长", "提示", JOptionPane.ERROR_MESSAGE);
					textArea.requestFocus();
					textArea.selectAll();
					return;
				}
				ArrayList<Department> arrayList = new ArrayList<Department>();
				arrayList = DepartmentDao.select();
				for (Department department : arrayList) {
					if (textField.getText().equals(department.getD_id())) {
						JOptionPane.showMessageDialog(null, "学院编号已存在", "提示", JOptionPane.ERROR_MESSAGE);
						textField.requestFocus();
						textField.selectAll();
						return;
					}
					if (textField_1.getText().equals(department.getD_name())) {
						JOptionPane.showMessageDialog(null, "学院名称已存在", "提示", JOptionPane.ERROR_MESSAGE);
						textField_1.requestFocus();
						textField_1.selectAll();
						return;
					}
				}
				Department department = new Department();
				department.setD_id(textField.getText());
				department.setD_name(textField_1.getText());
				department.setRemarks(textArea.getText());
				if (DepartmentDao.add(department)>0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					textField.setText(NumberGeneration.createDepartmentNum());
					textField_1.setText("");
					textArea.setText("");
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(DepartmentAddInterfrm.class.getResource("/images/add.png")));
		
		btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_1.setText("");
				textArea.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(DepartmentAddInterfrm.class.getResource("/images/resets.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(51)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_2)
									.addGap(18)
									.addComponent(textArea))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addGap(18)
									.addComponent(textField_1))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel)
									.addGap(18)
									.addComponent(textField, GroupLayout.PREFERRED_SIZE, 216, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(109)
							.addComponent(btnNewButton)
							.addGap(62)
							.addComponent(btnNewButton_1)))
					.addContainerGap(65, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(40)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(23))
		);
		getContentPane().setLayout(groupLayout);
		textField.setText(NumberGeneration.createDepartmentNum());
	}

}
